package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.util.*;
import org.example.util.DeviceUtil;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Calendar;
import java.util.Map;

import static org.example.enums.OnOffStatus.*;
@Service
@Slf4j
/**
 * 获取设备在线状态，6种
 */
public class OnOffStatusService {
    public static Integer getOnOffStatus( Map<String,Object> result,String imei){
        Integer onOffStatus=null;
        if(result.get("Life_ts")==null||result.get("firstError")==null||result.get("secondError")==null||result.get("minCellVoltage")==null){
            log.warn("No minCellVol or Error info, imei:"+imei);
            return null;
        }
        // 用于计算在线状态
        String lifeTs=(String)result.get("Life_ts");
        // 告警状态
        int[] firstAlarm = hexToBinary((String) result.get("firstError"), 32);
        int[] secondAlarm = hexToBinary((String) result.get("secondError"), 32);
        // 最小单体电压
        int minCellVol=(Integer)result.get("minCellVoltage");
        // 创建时间
        Long createdTs= DeviceUtil.getDeviceCreatedTime(imei);
        if(createdTs==null){
            log.warn("getOnOffStatus-createdTs:null, imei:"+imei);
            return null;
        }

        // 1.有欠压报警
        // bit i 对应下标为alarm.length - 1 - i=32-1-i=31-i
        //电芯欠压 bit 2对应29
        if(firstAlarm[29]==1||secondAlarm[29]==1){
            log.info("SingleVol_L,first:"+ result.get("firstError") +" second:"+ result.get("secondError"));
            // 1-1.若单体电压小于2500mv则为3馈电
            if(minCellVol<2500){
                log.info("Feed! minCellVol:"+result.get("minCellVoltage"));
                onOffStatus=FEED.onOffStatus;
            }
            else { // 1-2. 单体电压>=2500mv，则为欠压
                log.info("UnderVol! minCellVol:"+result.get("minCellVoltage"));
                onOffStatus=UNDERVOL.onOffStatus;
            }
        }
        else {
            // 2. 无欠压报警，设备新增(createdTime在24小时以内)
            long nowTs=System.currentTimeMillis();
            int totalDays=DateUtil.getDaysBetween(createdTs,nowTs);
            if(totalDays<1){// 2-1. 创建24h以内，新增
                log.info("NewAdd! createdTime:"+createdTs);
                onOffStatus=NEW_ADD.onOffStatus;
            }
            else { //2-2 创建24小时以外，正常判断
                log.info("judgeOnOffStatus! LifeTs:"+lifeTs);
                onOffStatus=judgeOnOffStatus(lifeTs);
            }
        }
        return onOffStatus;
    }

    public static int judgeOnOffStatus(String lifeTs){
        long oneMin=60000;
        long twoMin=120000;
        long fiveMin=5*oneMin;
        long fiveHour=5*60*oneMin;
        long oneDay=86400000;
        Calendar calendar = Calendar.getInstance();
        long nowTs=calendar.getTimeInMillis();
        long deviceTs=Long.parseLong(lifeTs);
        if((nowTs-deviceTs)<=fiveMin){// 5分钟以内，在线
            return ONLINE.onOffStatus;
        }
        else if((nowTs-deviceTs)<=fiveHour){// 5小时以内，休眠
            return SLEEP.onOffStatus;
        }
        else {// 5小时以外，离线
            return OFFLINE.onOffStatus;
        }

    }
    private static int[] hexToBinary(String hexString, int numBits) {
        int[] binaryArray = new int[numBits];
        String binaryString = new BigInteger(hexString, 16).toString(2);
        int binaryStringLength = binaryString.length();
        int numLeadingZeros = numBits - binaryStringLength;
        for (int i = 0; i < numLeadingZeros; i++) {
            binaryArray[i] = 0;
        }
        for (int i = numLeadingZeros; i < numBits; i++) {
            binaryArray[i] = binaryString.charAt(i - numLeadingZeros) - '0';
        }
        return binaryArray;
    }
}
